/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package interface_graphique;

import ClassesJava.Matieres;
import static interface_graphique.MenuPrincipale.menu;
import java.util.*;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author abdelfattah Rabou
 */
public class Matières extends javax.swing.JFrame {
    
    Matieres listm;
    DefaultTableModel model = new DefaultTableModel();
    int idm, j;

    /**
     * Creates new form Matières
     */
    public Matières() {
         initComponents();
         setLocationRelativeTo(null);
         model.addColumn("Numéro");
         model.addColumn("Label");
         model.addColumn("Nombre d'heures");
        listm = new Matieres();
         tabmatiere.setModel(model);
         afficher();
         afficher(0);
    }
    
    
    // afficher les matieres au tableau
    public void afficher (){    
        model.setRowCount(0);
        Vector<Matieres> res = new Vector();
        res = listm.getMatieres();
        for (int i = 0; i < res.size(); i++) {
            Object[] e = {res.get(i).getNum(), res.get(i).getLabel(), res.get(i).getNbr_heures()};
            model.addRow(e);
        }
        
    }
    
    
    // afficher une matière i 
    public void afficher(int i) {
        idm = (int) model.getValueAt(i, 0);
        txtnumero.setText(model.getValueAt(i, 0).toString());
        txtlabel.setText(model.getValueAt(i, 1).toString());
        txtnbr_heures.setText(model.getValueAt(i, 2).toString());
    }
    
    
    
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtnumero = new javax.swing.JTextField();
        txtlabel = new javax.swing.JTextField();
        txtnbr_heures = new javax.swing.JTextField();
        jBtPremiereElement = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jBtPressident = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jBtSuivent = new javax.swing.JButton();
        jBtDernierElement = new javax.swing.JButton();
        jBtModifier = new javax.swing.JButton();
        jBtNouveau = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tabmatiere = new javax.swing.JTable();
        jBtSupprimer = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jBtAjouter1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Captur1.PNG"))); // NOI18N
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 40, 290, 30));
        getContentPane().add(txtnumero, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 110, 120, -1));

        txtlabel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtlabelActionPerformed(evt);
            }
        });
        getContentPane().add(txtlabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 140, 120, -1));
        getContentPane().add(txtnbr_heures, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 170, 120, -1));

        jBtPremiereElement.setBackground(new java.awt.Color(0, 204, 255));
        jBtPremiereElement.setFont(new java.awt.Font("Tempus Sans ITC", 3, 12)); // NOI18N
        jBtPremiereElement.setForeground(new java.awt.Color(0, 102, 255));
        jBtPremiereElement.setText("<<");
        jBtPremiereElement.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtPremiereElementActionPerformed(evt);
            }
        });
        getContentPane().add(jBtPremiereElement, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 110, 50, 20));

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Capture4.PNG"))); // NOI18N
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 110, 230, 90));

        jBtPressident.setBackground(new java.awt.Color(0, 204, 255));
        jBtPressident.setFont(new java.awt.Font("Tempus Sans ITC", 3, 12)); // NOI18N
        jBtPressident.setForeground(new java.awt.Color(0, 102, 255));
        jBtPressident.setText("<");
        jBtPressident.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtPressidentActionPerformed(evt);
            }
        });
        getContentPane().add(jBtPressident, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 130, 50, 20));

        jButton4.setText("jButton1");
        getContentPane().add(jButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 110, 50, -1));

        jBtSuivent.setBackground(new java.awt.Color(0, 204, 255));
        jBtSuivent.setFont(new java.awt.Font("Tempus Sans ITC", 3, 12)); // NOI18N
        jBtSuivent.setForeground(new java.awt.Color(0, 102, 255));
        jBtSuivent.setText(">");
        jBtSuivent.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtSuiventActionPerformed(evt);
            }
        });
        getContentPane().add(jBtSuivent, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 150, 50, 20));

        jBtDernierElement.setBackground(new java.awt.Color(0, 204, 255));
        jBtDernierElement.setFont(new java.awt.Font("Tempus Sans ITC", 3, 12)); // NOI18N
        jBtDernierElement.setForeground(new java.awt.Color(0, 102, 255));
        jBtDernierElement.setText(">>");
        jBtDernierElement.setActionCommand("");
        jBtDernierElement.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtDernierElementActionPerformed(evt);
            }
        });
        getContentPane().add(jBtDernierElement, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 170, 50, 20));

        jBtModifier.setBackground(new java.awt.Color(0, 204, 255));
        jBtModifier.setFont(new java.awt.Font("Tempus Sans ITC", 3, 12)); // NOI18N
        jBtModifier.setForeground(new java.awt.Color(51, 102, 255));
        jBtModifier.setText("MODIFIER");
        jBtModifier.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtModifierActionPerformed(evt);
            }
        });
        getContentPane().add(jBtModifier, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 240, 100, 30));

        jBtNouveau.setBackground(new java.awt.Color(0, 204, 255));
        jBtNouveau.setFont(new java.awt.Font("Tempus Sans ITC", 3, 12)); // NOI18N
        jBtNouveau.setForeground(new java.awt.Color(51, 102, 255));
        jBtNouveau.setText("NOUVEAU");
        jBtNouveau.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtNouveauActionPerformed(evt);
            }
        });
        getContentPane().add(jBtNouveau, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 240, 110, 30));

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/booksspece.jpg"))); // NOI18N
        jLabel3.setText("jLabel3");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(430, 0, 230, -1));

        tabmatiere.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(0, 255, 204)));
        tabmatiere.setFont(new java.awt.Font("Tempus Sans ITC", 3, 12)); // NOI18N
        tabmatiere.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Numéro ", "Label", "Nombre d'heures"
            }
        ));
        tabmatiere.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabmatiereMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tabmatiere);

        getContentPane().add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 330, 300, 140));

        jBtSupprimer.setBackground(new java.awt.Color(0, 204, 255));
        jBtSupprimer.setFont(new java.awt.Font("Tempus Sans ITC", 3, 12)); // NOI18N
        jBtSupprimer.setForeground(new java.awt.Color(51, 102, 255));
        jBtSupprimer.setText("SUPPRIMER");
        jBtSupprimer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtSupprimerActionPerformed(evt);
            }
        });
        getContentPane().add(jBtSupprimer, new org.netbeans.lib.awtextra.AbsoluteConstraints(530, 240, -1, 30));

        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Capture8.PNG"))); // NOI18N
        jLabel6.setText("jLabel6");
        getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 290, 190, -1));

        jBtAjouter1.setBackground(new java.awt.Color(0, 204, 255));
        jBtAjouter1.setFont(new java.awt.Font("Tempus Sans ITC", 3, 12)); // NOI18N
        jBtAjouter1.setForeground(new java.awt.Color(51, 102, 255));
        jBtAjouter1.setText("AJOUTER");
        jBtAjouter1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtAjouter1ActionPerformed(evt);
            }
        });
        getContentPane().add(jBtAjouter1, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 240, 110, 30));

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/fond_ecran_wallpaper_25.jpg"))); // NOI18N
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 660, 500));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtlabelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtlabelActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtlabelActionPerformed

    private void jBtPremiereElementActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtPremiereElementActionPerformed
        j = 0;
        afficher(j);       
    }//GEN-LAST:event_jBtPremiereElementActionPerformed

    private void jBtPressidentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtPressidentActionPerformed
        if (j > 0) {
            j--;
            afficher(j);
        }
    }//GEN-LAST:event_jBtPressidentActionPerformed

    private void jBtSuiventActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtSuiventActionPerformed
        if (j < tabmatiere.getRowCount() - 1) {
            j++;
            afficher(j);
        }        
    }//GEN-LAST:event_jBtSuiventActionPerformed

    private void jBtDernierElementActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtDernierElementActionPerformed
         j = tabmatiere.getRowCount() - 1;
             afficher(j);        
    }//GEN-LAST:event_jBtDernierElementActionPerformed

    private void jBtSupprimerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtSupprimerActionPerformed
        //botton supprimer
          int i;
        Matieres m;
        m = new Matieres(idm, txtlabel.getText(), Integer.parseInt(txtnbr_heures.getText()));
        i = m.supprimer(m);
        if (i == 1) {
            afficher();
            javax.swing.JOptionPane.showMessageDialog(null, "Une matiere est bien Supprimer", "Supprimer une matiere", javax.swing.JOptionPane.INFORMATION_MESSAGE);
        } else {
            javax.swing.JOptionPane.showMessageDialog(null, "echec de supprimer matiere", "Supprimer une matiere", javax.swing.JOptionPane.INFORMATION_MESSAGE);
        }

    }//GEN-LAST:event_jBtSupprimerActionPerformed

    private void jBtModifierActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtModifierActionPerformed
        // booton Modifier
         int i;
        Matieres m;
        m = new Matieres(idm, txtlabel.getText(), Integer.parseInt(txtnbr_heures.getText()));
        i = m.modifier(m);
        if (i == 1) {
            afficher();
            javax.swing.JOptionPane.showMessageDialog(null, "La matière est bien Modifier", "Modifier une Matière", javax.swing.JOptionPane.INFORMATION_MESSAGE);
        } else {
            javax.swing.JOptionPane.showMessageDialog(null, "echec de Modifier la matière", "Modifier une matière", javax.swing.JOptionPane.INFORMATION_MESSAGE);
        }

       
    }//GEN-LAST:event_jBtModifierActionPerformed

    private void jBtNouveauActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtNouveauActionPerformed
        // booton nouveau
        txtnumero.setText("");
        txtlabel.setText("");
        txtnbr_heures.setText("");
    }//GEN-LAST:event_jBtNouveauActionPerformed

    private void tabmatiereMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabmatiereMouseClicked
         j = tabmatiere.getSelectedRow();
        afficher(j);
    }//GEN-LAST:event_tabmatiereMouseClicked

    private void jBtAjouter1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtAjouter1ActionPerformed
         int i;
        Matieres m;
        m = new Matieres(0, txtlabel.getText(), Integer.parseInt(txtnbr_heures.getText()));
        i = m.ajouter(m);
        if (i == 1) {
            afficher();
            javax.swing.JOptionPane.showMessageDialog(null, "Une nouvelle matiere est bien enregistré", "Ajouter un Matiere", javax.swing.JOptionPane.INFORMATION_MESSAGE);
        } else {
            javax.swing.JOptionPane.showMessageDialog(null, "echec d'ajouter une nouelle matière", "Ajouter une matière", javax.swing.JOptionPane.INFORMATION_MESSAGE);
        }
        
    }//GEN-LAST:event_jBtAjouter1ActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
          menu.setVisible(true);
    }//GEN-LAST:event_formWindowClosed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Matières.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Matières.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Matières.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Matières.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Matières().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtAjouter1;
    private javax.swing.JButton jBtDernierElement;
    private javax.swing.JButton jBtModifier;
    private javax.swing.JButton jBtNouveau;
    private javax.swing.JButton jBtPremiereElement;
    private javax.swing.JButton jBtPressident;
    private javax.swing.JButton jBtSuivent;
    private javax.swing.JButton jBtSupprimer;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tabmatiere;
    private javax.swing.JTextField txtlabel;
    private javax.swing.JTextField txtnbr_heures;
    private javax.swing.JTextField txtnumero;
    // End of variables declaration//GEN-END:variables
}
